﻿//  --------------------------------
//  Copyright (c) AW Systems, Inc. All rights reserved.
//  This source code is made available under the terms of the Microsoft Public License (Ms-PL)
//  http://www.codeplex.com/SLAM/license
//  ---------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AWS.SLAM.Configuration;
using AWS.SLAM.Data;
using AWS.SLAM.Logging;
using AWS.SLAM.SharePoint;
using Microsoft.SharePoint;

namespace AWS.SLAM.Events
{
    /// <summary>
    /// ListItemDeleter deletes an item to SLAM's datasource(s) when a new List Item is deleted from a 
    /// List configured to be managed by SLAM in SharePoint.
    /// 
    /// ListItemDeleter is the default implementation of SLAM's IItemDeleter that uses a List Item's
    /// List to determine its behavior.
    /// </summary>
    public class ListItemDeleter : IItemDeleter
    {
        #region Private State 
       
        private IDataServices _dataServices;
        private IConfigurationManager _configuration;        
        private ILogger _logger;

        #endregion

        #region Constructors

        public ListItemDeleter(IDataServices dataServices, IConfigurationManager configuration, ILogger logger)
        {
            _dataServices = dataServices;
            _configuration = configuration;            
            _logger = logger;
        }

        #endregion

        #region IItemDeleter Members

        public virtual bool Delete(SPListItem listItem)
        {
            bool isInError = false;
            string parentListId = listItem.ParentList.ID.ToString();

            try
            {
                string logItemInformation = listItem.ParentList.Title + Environment.NewLine + " List: " + listItem.ParentList.Title + Environment.NewLine + "ListItem: " + listItem.GetTitle() + ", " + listItem.UniqueId.ToString();
                _logger.LogMessage("Information", "Delete LI Start - " + logItemInformation);

                string itemTypeName = _configuration.DataMapping.GetTypeAttribute(parentListId, listItem.ParentList.Title, "TableName");
                if (String.IsNullOrEmpty(itemTypeName))
                {
                    itemTypeName = listItem.ParentList.Title;
                }

                _dataServices.Delete(itemTypeName, listItem.UniqueId.ToString());

                _logger.LogMessage("Information", "Delete LI Complete - " + logItemInformation);

                List<string> clearedAssociations = new List<string>();

                _logger.LogMessage("Information", "Delete LI Associations - " + logItemInformation);

                foreach (AssociationField association in _configuration.DataMapping.GetTypeAssociations(parentListId, listItem.ParentList.Title))
                {
                    if (listItem.Fields[association.Name] != null)
                    {
                        if (!clearedAssociations.Contains(association.AssociationName))
                        {
                            _logger.LogMessage("Information", "Delete LI Associations - For: " + itemTypeName + " Type: " + association.AssociationName);
                            _dataServices.DeleteAllAssociations(association.AssociationName, itemTypeName, listItem.UniqueId.ToString());
                            clearedAssociations.Add(association.AssociationName);
                        }
                    }
                }

                _logger.LogMessage("Information", "Delete LI Associations Complete - " + logItemInformation);
            }
            catch (Exception ex)
            {
                _logger.LogMessage("Exception", ex.ToString());
                isInError = true;
            }

            _logger.LogEvent("Delete", listItem.ParentList.Title, "List: " + listItem.ParentList.Title + Environment.NewLine + "ListItem: " + listItem.GetTitle() + ", " + listItem.UniqueId.ToString());

            return !isInError;
        }

        #endregion
    }
}
